# Part of Cosmos by OpenGenus Foundation
def bubble_sort(array)
  n = array.length
  loop do
    swapped = false

    (n - 1).times do |i|
      if array[i] > array[i + 1]
        array[i], array[i + 1] = array[i + 1], array[i]
        swapped = true
      end
    end

    break unless swapped
  end

  array
end

an_array = [5, 1, 3, 9, 2]
puts bubble_sort an_array
